var token = localStorage.getItem('token'),
    finalExam = Sendajax("http://119.23.46.241:8080/login/finalExam", "post", {time: "1-1"}, token),
    num = 0,
    schoolYearArr = ['2019-2020学年第1学期', '2019-2020学年第2学期', '2020-2021学年第1学期', '2020-2021学年第2学期',
        '2021-2022学年第1学期', '2021-2022学年第2学期', '2022-2023学年第1学期', '2022-2023学年第2学期'
    ],
    schoolYearNum = ['1-1', '1-2', '2-1', '2-2', '3-1', '3-2', '4-1', '4-2'];


setTimeout(function () {
    $(".school-year-text p").text(schoolYearArr[num])
    exam(finalExam)
}, 100)
localStorage.setItem('isViewFinalGrade', 1)
if (localStorage.getItem('isCheckDormitoryResults')) {
    $(".back").on("click", function () {
        window.history.back(-2)
    })
}

//显示成绩
function exam(data) {
    let examData = ''
    if (data.code == 502) {
        examData += `<section class="no-content">
        <section></section>
         <section>
         <p>成绩表未上传或错误</p>
         <p>请联系管理员上传或修改</p>
         </section>
         </section>`
    }
    if (data.code == 500) {
        examData += `<section class="no-content">
        <section></section>
         <p>暂无内容</p>
         </section>`
    }
    if (data.code == 201) {
        if (data.data.length <= 1) {
            examData += `<section class="no-content">
        <section></section>
         <p>暂无内容</p>
         </section>`
        } else {
            $.each(data.data.score, function (i, val) {
                examData += `<section class="grade">
            <section class="fraction">
                <p>成绩</p>
                <p>${val.score}</p>
            </section>
            <section class="subject">
                <p class="subject-name">${val.subject}</p>
                <p class="subject-teacher">${val.teacher}</p>
            </section>
        </section>`
            })
            examData += `        <section class="ranking-list">
            <section class="total-score">
                <p>总分</p>
                <i>${data.data.info.zongfen}</i>
            </section>
            <section class="ranking">
                <p>排行</p>
               <i>${data.data.info.paimin}</i>
            </section>
            <section class="name">
                <p>姓名</p>
                <i>${data.data.info.name}</i>
            </section>
        </section>
        <section class="download-button">
            <section class="download">
                <a >下载成绩单</a>
            </section>
        </section>
`
        }
    }
    $(".grade-content").html(examData)
    $(".download a").on('click', function () {
        window.location.href = data.data.info.url
    })
}

//下一学期
$(".next-school-year").on("click", function () {
    num++
    if (num >= 8) {
        num = 0
        $(".school-year-text p").text(schoolYearArr[num])
    } else {
        $(".school-year-text p").text(schoolYearArr[num])
    }
    finalExam = Sendajax("http://119.23.46.241:8080/login/finalExam", "post", {time: schoolYearNum[num]}, token)
    exam(finalExam)
})

//上一学期
$(".last-school-year").on("click", function () {
    num--
    if (num < 0) {
        num = 7
        $(".school-year-text p").text(schoolYearArr[num])
    } else {
        $(".school-year-text p").text(schoolYearArr[num])
    }
    finalExam = Sendajax("http://119.23.46.241:8080/login/finalExam", "post", {time: schoolYearNum[num]}, token)
    exam(finalExam)
})


$(".search-box input").keydown(function (e) {
    if (e.keyCode == 13) {
        let searchData = Sendajax("http://119.23.46.241:8080/login/findfe", "post", {
            time: schoolYearNum[num],
            input: $(this).val()
        }, token)
        $(".search-box input").blur()
        exam(searchData)
    }
})
